/**
 * 
 */
package com.hsxc.cdz.controller.wxController.jssdk;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.hsxc.cdz.controller.wxController.entity.Openid;
import com.hsxc.cdz.controller.wxController.util.Constants;
import com.hsxc.cdz.controller.wxController.util.Getvar;
import com.hsxc.cdz.controller.wxController.util.PropertiesUtil;

/** 
* @author  wpfei
* @version 创建时间：2016年6月30日 下午5:11:54 
* 类说明 ：
*/

public class Jssdk {

	/**
	 * 
	 */
    public static String  jsapi_ticket = "";
	public static String url = "";
	public static String code ="" ;
	
	public static Map<String, String> putdata() {
		Map<String, String> ret = scan(jsapi_ticket, url);
		return ret;

	}

	public static Map<String, String> scan(String jsapi_ticket, String url) {
		
		PropertiesUtil putil=new PropertiesUtil(Constants.CONF_PROPERTIES_PATH);
		
		Map<String, String> ret = new HashMap<String, String>();
		String nonce_str = create_nonce_str();
		String timestamp = create_timestamp();
		// Getvar get = new Getvar();
		//bug dead parameter 
		jsapi_ticket=putil.getProperty("JSAPI_TICKET");
		 System.out.println(jsapi_ticket);
		String accesstoken = putil.getProperty("ACCESS_TOKEN");
		System.out.println(accesstoken);
		Openid  open =	Getvar.getOpenid() ;
		String openid = open.getOpenid();
		System.out.println(openid);
		String string1;
		String signature = "";

		// 濞夈劍鍓版潻娆撳櫡閸欏倹鏆熼崥宥呯箑妞よ鍙忛柈銊ョ毈閸愭瑱绱濇稉鏂跨箑妞ょ粯婀佹惔锟�
		string1 = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url;
		System.out.println(string1);

		try {
			MessageDigest crypt = MessageDigest.getInstance("SHA-1");
			crypt.reset();
			crypt.update(string1.getBytes("UTF-8"));
			signature = byteToHex(crypt.digest());
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		ret.put("openid", openid);
		ret.put("accesstoken", accesstoken);
		ret.put("url", url);
		ret.put("jsapi_ticket", jsapi_ticket);
		ret.put("nonceStr", nonce_str);
		ret.put("timestamp", timestamp);
		ret.put("signature", signature);
		return ret;
	}

	public static String byteToHex(final byte[] hash) {
		Formatter formatter = new Formatter();
		for (byte b : hash) {
			formatter.format("%02x", b);
		}
		String result = formatter.toString();
		formatter.close();
		return result;
	}

	public static String create_nonce_str() {

		// return UUID.randomUUID().toString();
		String noncetemp = UUID.randomUUID().toString();
		String nonce_str = "";
		char[] noncechar = noncetemp.toCharArray();
		for (int i = 0; i < noncechar.length; i++)
			if (noncechar[i] != '-')
				nonce_str += noncechar[i];
		return nonce_str.substring(0, 16);
	}

	public static String create_timestamp() {
		return Long.toString(System.currentTimeMillis() / 1000);
	}

	public static void putUrl(String input_url) {
		url = input_url;
		System.out.println("Input url is " + url);
	}

	public static void putCode(String input_code) {
		code = input_code;
		System.out.println("Input url is " + code);
	}
	public static  String getCode()
	{
		return code;
	}

}
